<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <title>登录</title>
    <link rel="stylesheet" href="./component/pear/css/pear.css"/>
    <link rel="stylesheet" href="./admin/css/other/login.css"/>
    <link rel="stylesheet" href="./admin/css/variables.css"/>
    <script>if (window.self != window.top) {
        top.location.reload();
    }</script>
</head>

<body>
<div class="login-page">
    <div class="layui-row">
        <div class="layui-col-sm6 login-bg layui-hide-xs">
            <img class="login-bg-img" src="./admin/images/banner.png" alt=""/>
        </div>
        <div class="layui-col-sm6 layui-col-xs12 login-form">
            <div class="layui-form">
                <div class="form-center">
                    <div class="form-center-box">
                        <div class="top-log-title">
                            <img class="login-bg-img" src="admin/images/logo.png" alt="" style="width: 50%"/>
                        </div>
                        <div class="top-desc" style="font-size: 18px;color: #7bc2f3;">
                            材料报表系统
                        </div>
                        <div style="margin-top: 60px;">
                            <div class="layui-form-item">
                                <div class="layui-input-wrap">
                                    <div class="layui-input-prefix">
                                        <i class="layui-icon layui-icon-username"></i>
                                    </div>
                                    <input lay-verify="required" placeholder="账户" autocomplete="off" id="loginName"
                                           name="loginName"
                                           class="layui-input">
                                </div>
                            </div>
                            <div class="layui-form-item">
                                <div class="layui-input-wrap">
                                    <div class="layui-input-prefix">
                                        <i class="layui-icon layui-icon-password"></i>
                                    </div>
                                    <input type="password" name="password" value=""
                                           lay-verify="required|confirmPassword" placeholder="密码" autocomplete="off"
                                           id="password"
                                           class="layui-input" lay-affix="eye">
                                </div>
                            </div>
                            <div class="tab-log-verification">
                                <div class="verification-text">
                                    <div class="layui-input-wrap">
                                        <div class="layui-input-prefix">
                                            <i class="layui-icon layui-icon-auz"></i>
                                        </div>
                                        <input lay-verify="required" value="" placeholder="验证码" autocomplete="off"
                                               id="randomCode" name="randomCode"
                                               class="layui-input">
                                        <input type="hidden" id="codeKey" name="codeKey">

                                    </div>
                                </div>
                                <img src="" style="width: 40%;margin-left:10px;float:right;cursor:pointer" id="vCode">
                            </div>
                            <div class="layui-form-item">
                            </div>
                            <div class="login-btn">
                                <button type="button" lay-submit lay-filter="login" class="layui-btn login"
                                        style="background-color:#4e9ef3;cursor:pointer">登 录
                                </button>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>


<form class="layui-form" action="" lay-filter="cpForm" id="cpForm" style="display: block">
    <!--    <div style="margin-top: 15px;margin-bottom: 15px;" id="cpDiv">-->
    <!--    </div>-->
    <div id="cpTree" class="demo-tree"></div>
</form>

<!-- 资 源 引 入 -->
<script src="./component/layui/layui.js"></script>
<script src="./component/pear/pear.js"></script>
<script src="./common/common-config.js"></script>

<script>
    layui.use(['form', 'button', 'popup', 'tree', 'http', 'pub', 'jquery', 'shopx'], function () {
        var form = layui.form;
        var button = layui.button;
        var popup = layui.popup;
        var http = layui.http;
        var pub = layui.pub;
        var $ = layui.jquery;
        var tree = layui.tree;
        var shopx = layui.shopx;

        //初始化验证码
        var codeKey = pub.genRandomString(16);
        $("#codeKey").val(codeKey);
        $("#vCode").attr("src", serverPath + "/sysUser/genRandomCode?codeKey=" + codeKey);

        $("#vCode").click(function () {
            var codeKey = pub.genRandomString(16)
            $("#codeKey").val(codeKey);
            if ($("#vCode").attr("src").indexOf("?") > -1) {
                $("#vCode").attr("src", $("#vCode").attr("src").substr(0, $("#vCode").attr("src").indexOf("?")) + "?codeKey=" + codeKey)
            } else {
                $("#vCode").attr("src", $("#vCode").attr("src") + "?codeKey=" + codeKey);
            }
        });

        // 登 录 提 交
        form.on('submit(login)', function (data) {
            var model = data.field;
            http.postRequestNoLogin("/sysUser/selectUserCpList", 'POST', model, function suc(data) {
                if (data.rspData.length == 0) {
                    layer.msg("对不起，您没权限访问系统");
                    return false;
                } else if (data.rspData.length == 1) {
                    //直接登录
                    model['cpId'] = data.rspData[0].cpId;
                    http.postRequestNoLogin("/sysUser/officeLogin", 'POST', model, function suc(data) {
                        shopx.setUserTokenId(data.rspData.tokenId);
                        shopx.setUserInfo(data.rspData);
                        if (data.rspData.initPswdFlag == "1") {
                            //初始化密码，需要修改
                            layer.confirm('提示，系统检测到您的密码为初始密码，请修改密码后登录！', {
                                btn: ['确定']
                            }, function () {
                                location.href = "./change_pswd.html"
                            });
                        } else {
                            //登录成功，跳转到首页
                            location.href = "./index.html"
                        }
                    });
                } else {
                    //选择公司进行登录
                    //渲染模板
                    $("#cpDiv").html('');
                    //查询企业树菜单
                    var cpIds = "";
                    $(data.rspData).each(function (idx, item) {
                        cpIds += item.cpId + ",";
                    });
                    if (!pub.isEmpty(cpIds)) {
                        cpIds = cpIds.substr(0, cpIds.length - 1);
                    }
                    var model2 = {
                        cpId: cpIds
                    }
                    http.postRequestNoLogin("/sysCompany/selectCpTree", 'POST', model2, function suc(data) {
                        // console.info(JSON.stringify(data.rspData));
                        tree.render({
                            elem: '#cpTree'
                            , data: data.rspData
                            , onlyIconControl: false  //是否仅允许节点左侧图标控制展开收缩
                            , click: function (obj) {
                                // layer.msg(JSON.stringify(obj.data));
                                if (obj.data.level == 3) {
                                    model['cpId'] = obj.data.id;
                                    http.postRequestNoLogin("/sysUser/officeLogin", 'POST', model, function suc(data) {
                                        shopx.setUserTokenId(data.rspData.tokenId);
                                        shopx.setUserInfo(data.rspData);
                                        if (data.rspData.initPswdFlag == "1") {
                                            //初始化密码，需要修改
                                            location.href = "./change_pswd.html"
                                        } else {
                                            //登录成功，跳转到首页
                                            location.href = "./index.html"
                                        }
                                    }, null, false, false);
                                }
                            }
                        });
                        layer.open({
                            title: '选择登录企业',
                            type: 1,
                            skin: 'layui-layer-border',
                            area: ['400px', '480px'],
                            content: $('#cpForm'),
                            maxmin: false,
                            shade: false,
                            minStack: false, // 最小化不堆叠在左下角
                            id: 'page-1', // 定义 ID，防止重复弹出
                        });
                    });
                }
                form.render();
                return false;
            }, null, false, false);

            return false;
        });
    })
</script>
</body>

</html>
